package com.sky.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insertSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);//属性拷贝
        setmealMapper.insertSetmeal(setmeal);//向套餐表插入信息
        //获取套餐id
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();//插入套餐中的菜品
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        setmealDishMapper.insert(setmealDishes);//要动态插入
    }
    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开始分页
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(setmealPageQueryDTO.getCategoryId());
        setmeal.setName(setmealPageQueryDTO.getName());
        setmeal.setStatus(setmealPageQueryDTO.getStatus());

        Page<SetmealDTO> page= setmealMapper.pageQuery(setmeal);

        return new PageResult<>(page.getTotal(),page.getResult());
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    @Transactional
    public SetmealVO getByid(Long id) {
        //查询套餐信息
        SetmealVO setmealVO =setmealMapper.getByid(id);
        //查套餐对应的菜品 一对多
        List<SetmealDish> setmealDishes  = setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    @Transactional
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);//属性拷贝
        setmealMapper.updateSetmeal(setmeal);
        //修改套餐对应的菜品
        //思路 直接删除之前的菜品 然后 在增加修改后的菜品
        Long setmealId = setmeal.getId();
        setmealDishMapper.deleteById(setmealId);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //插入之前 设置套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        //插入菜品
        setmealDishMapper.insert(setmealDishes);

    }
    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBarch(List<Long> ids) {
        //两种方法删除 一种是遍历集合一个一个删 另一种是 在mapeer中遍历 删除
            //删除套餐
          setmealMapper.deleteBarch(ids);
            //删除套餐对应的菜品
            setmealDishMapper.deleteBarch(ids);
    }

    /**
     * 设置店铺状态
     * @param status
     * @param id
     */

    @Override
    public void updateStatus(Integer status, Long id) {
        setmealMapper.updateSetmeal(Setmeal.builder().id(id).status(status).build());
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
